package ModelLayer;
import java.util.ArrayList;

public class LoanContainer
{
    private static LoanContainer instance;
    
    private ArrayList<Loan> loanList;
    
    private LoanContainer()
    {
        loanList = new ArrayList<Loan>();
    }
    
    public static LoanContainer getInstance()
    {
        if(instance == null)
        {
            instance = new LoanContainer();
        }
                
        return instance;
    }

    //Adds an object of type Loan to the ArrayList
    public void addLoan(Loan newLoan)
    {            
        loanList.add(newLoan);
        System.out.println("Loan has been created!");
    }
    
    //Search and print
    public void getLoan(long id)
    {
        boolean found = false;
        int index = 0;
        while(index < loanList.size() && found == false)
        {
            String id1 = String.valueOf(id);
            String idIndex = String.valueOf(loanList.get(index).getId());
            
            if(idIndex.equals(id1))
            {
                loanList.get(index).getInfo();
                found = true;
            }
            else
            {
                index++;
            }
        }
        if(found==false)
            {
                System.out.println("You don't have a loan with that ID... maybe you should check the loan-list?");
            }
    }
    
    //Search and delete
    public void deleteLoan(long id)
    {
        int index = 0;
        boolean found = false;

        while(index < loanList.size() && found == false)
        {        
            String id1 = String.valueOf(id);
            String idIndex = String.valueOf(loanList.get(index).getId());
                        
            if(idIndex.equals(id1))
            {
                loanList.remove(index);
                System.out.println("Loan has been deleted!");
                found = true;
            }
            else
            {
                index++;
            }
        }
        if(found==false)
            {
                System.out.println("You don't have a loan with that ID... maybe you should check the loan-list?");
            }
    }
    
    //Search and update
    public void updateLoan(String date, int periode, boolean status, long id)
    {
        int index = 0;
        boolean found = false;

        while(index < loanList.size() && found == false)
        {
            String id1 = String.valueOf(id);
            String idIndex = String.valueOf(loanList.get(index).getId());
                        
            if(idIndex.equals(id1))
            {
                loanList.get(index).changeInfo(date, periode, status, id);
                found = true;
                System.out.println("Loan has been updated!");
            }
            else
            {
                index++;
            }
        }
        if(found==false)
            {
                System.out.println("You don't have a loan with that ID to update... maybe you should check your loan-list?");
            }
    }
    
    //Prints the information 'for each' object in the ArrayList loanList
    public void listAllLoans()
    {       
        for(Loan loan : loanList)
        {
            System.out.println("--------");
            loan.getInfo();
        }
        System.out.println("--------");
    }
}
